package com.xinghou.platform.dao.wzk.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.xinghou.platform.dao.bases.JdbcTemplate;
import com.xinghou.platform.dao.bases.database.CustomerContextHolder;
import com.xinghou.platform.dao.wzk.ReqcardDAO;
import com.xinghou.platform.model.wzk.Reqcard;

@Repository
public class ReqcardDAOImpl implements ReqcardDAO {
	@Resource
	private JdbcTemplate jdbcTemplate;

	@Override
	public Reqcard getReqcardById(String id) throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		String sql = "SELECT id, content, cityID, TYPE, sex, shopname, state, store_id, store_type FROM wzk.reqcard where id = ?";
		Reqcard reqcard = jdbcTemplate.queryForObject(sql, new Object[] { id }, new ReqcardMapper());
		CustomerContextHolder.clearCustomerType();
		return reqcard;
	}

	@Override
	public void updateReqcard(Reqcard reqcard) throws DataAccessException {
		StringBuffer strSQL = new StringBuffer("UPDATE wzk.reqcard SET content = ?,cityID = ?, TYPE = ?, sex = ?, shopname = ?, state = ?, store_id = ?, store_type = ?")
		.append(" where id=?");
		
       Object[] params = {reqcard.getContent(),reqcard.getCityId(),reqcard.getType(),reqcard.getSex(),reqcard.getShopname(),reqcard.getState(),
    		   reqcard.getStore_id(),reqcard.getStore_type(),reqcard.getId()   
     };

    CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_WRITE);
    int ret = jdbcTemplate.update(strSQL.toString(),params);
    CustomerContextHolder.clearCustomerType();
		
	}
	
	private static final class ReqcardMapper implements RowMapper<Reqcard> {
		public Reqcard mapRow(ResultSet rs, int rowNum) throws SQLException {
			Reqcard reqcard = new Reqcard();
			reqcard.setCityId(rs.getString("cityId"));
			reqcard.setContent(rs.getString("content"));
			reqcard.setId(rs.getInt("id"));
			reqcard.setSex(rs.getInt("sex"));
			reqcard.setShopname(rs.getString("shopname"));
			reqcard.setState(rs.getInt("state"));
			reqcard.setStore_id(rs.getInt("store_id"));
			reqcard.setStore_type(rs.getInt("store_type"));
			reqcard.setType(rs.getInt("type"));
			return reqcard;
		}
	}
	
}
